package utils;


import com.aliyun.oss.OSSClient;
import com.aliyun.oss.internal.Mimetypes;
import com.aliyun.oss.model.ObjectMetadata;
import constants.Configurations;
import constants.Default;
import play.Logger;
import play.Play;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author yangzhi
 * @date 2018/12/8
 */
public class OssUtils {

	static String ossBucketImage= Configurations.ossBucketImg;
	static String ossBucketVideo= Configurations.ossBucketVideo;

	/**
	 * 上传图片到 oss
	 * @param fileKey
	 * @param file
	 * @return
	 */
	public static boolean uploadImageToOss(String fileKey, File file) {
		AliyunOSSContext context = new AliyunOSSContext();
		context.bucket = ossBucketImage;
		return OssUtils.uploadToOss(fileKey, file, context);
	}

	/**
	 * 删除oss物理图片
	 * @param fileKey
	 * @return
	 */
	public static boolean deleteImageFromOss(String fileKey) {
		AliyunOSSContext context = new AliyunOSSContext();
		context.bucket = ossBucketImage;
		//删除图片
		Boolean result = false;
		Boolean delFlag=delete(context,fileKey);
		Logger.info("deleteImageFromOss:"+fileKey+" delFlag:"+delFlag);
		if(delFlag){
			//刷新缓存
			result=refresh(context,fileKey,Default.typeId.image);
			Logger.info("deleteImageFromOss:"+fileKey+" refreshFlag:"+result);
		}
		if (result && delFlag) {
			return true;
		}else{
			return  false;
		}
	}


	/**
	 * 删除物理oss视频
	 * @param fileKey
	 * @return
	 */
	public static boolean deleteVideoFromOss(String fileKey) {
		AliyunOSSContext context = new AliyunOSSContext();
		context.bucket = ossBucketVideo;
		//删除视频
		Boolean result = false;
		Boolean delFlag=delete(context,fileKey);
		Logger.info("deleteVideoFromOss:"+fileKey+"delFlag:"+delFlag);
		if(delFlag){
			//刷新缓存
			result=refresh(context,fileKey,Default.typeId.video);
			Logger.info("deleteVideoFromOss:"+fileKey+"refreshFlag:"+result);
		}
		if (result && delFlag) {
			return true;
		}else{
			return  false;
		}
	}


	private static boolean uploadToOss(String fileKey, File file, AliyunOSSContext context) {
		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(context.endPoint, context.accessKeyId, context.accessKeySecret);
		InputStream inputStream = null;
		try {
			// 创建上传Object的Metadata
			ObjectMetadata _meta = new ObjectMetadata();
			_meta.setContentType(Mimetypes.getInstance().getMimetype(file));
			_meta.setContentLength(file.length());
			// 上传字符串
			inputStream = new FileInputStream(file);
			ossClient.putObject(context.bucket, fileKey, inputStream);
			return true;
		} catch (Exception e) {
			Logger.error(e, "Failed to upload file to Aliyun OSS");
			return false;
		} finally {
			// 关闭client
			ossClient.shutdown();
			IOUtils.closeQuietly(inputStream);
		}
	}


	private static boolean delete(AliyunOSSContext context, String fileKey) {
		String endpoint = context.endPoint;
		String accessKeyId = context.accessKeyId;
		String accessKeySecret = context.accessKeySecret;
		String bucket = context.bucket;

		// 创建OSSClient实例
		OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
		try{
			// 删除Object
			ossClient.deleteObject(bucket, fileKey);
			return true;
		}catch (Exception e){
			Logger.error(e, "Failed to delete file from Aliyun OSS");
			return false;
		}finally {
			// 关闭client
			ossClient.shutdown();
		}
	}


	private static boolean refresh(AliyunOSSContext context, String fileKey,Integer typeId) {
		try{

			String fileKeyUrl = "";
			if (typeId.equals(Default.typeId.image)){
				fileKeyUrl =Configurations.ossBucketImgUrl+fileKey;
			}else{
				fileKeyUrl =Configurations.ossBucketVideoUrl+fileKey;
			}
			String r = AliyunCdnUtils.refreshObjectCaches(fileKeyUrl);
			Logger.info(r.toString());
			// 创建OSSClient实例

			// 删除Object
			return true;
		}catch (Exception e){
			Logger.error(e, "Failed to refresh file from Aliyun OSS");
			return false;
		}finally {
			// 关闭client
		}
	}
}
